/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.core.resources;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.resources.IFile;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class LocalFile extends LocalResource
{
	/**
	  * Constructor for LocalFile.
	  * @param ioResource
	  */
	LocalFile(IFile file)
	{
		super(file);
	}

	public ResourceSyncInfo getSyncInfo()
	{
		ResourceSyncInfo syncInfo = null;
		try
		{
			syncInfo = PerforceUIPlugin.getSynchronizer().getResourceSync(toResource());
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.getPlugin().log(e.getStatus());
		}
		
		return syncInfo == null ? ResourceSyncInfo.NULL_INFO : syncInfo;
	}


	public boolean hasRemote()
	{
		return !getSyncInfo().isNull();
	}

	public boolean isCheckedOut()
	{
		ResourceSyncInfo syncInfo = getSyncInfo();
		return !syncInfo.isNull() && (syncInfo.getAction().equals(ResourceSyncInfo.ACTION_EDIT) || syncInfo.getAction().equals(ResourceSyncInfo.ACTION_ADD));
	}
	
	public boolean isDirty()
	{
		return isCheckedOut();
//		Date fileTime = new Date(((IFile)toResource()).getLocation().toFile().lastModified());
//		
//		ResourceSyncInfo syncInfo = getSyncInfo();
//		return syncInfo != ResourceSyncInfo.NULL_INFO ? syncInfo.getHeadTime().before(fileTime) : false;
	}
		
	public boolean isOutOfDate()
	{
		ResourceSyncInfo syncInfo = getSyncInfo();
		
		boolean result = false;
		if (syncInfo != null)
		{
			result = syncInfo.isResolved().equals(Boolean.FALSE);
			result = result || (!syncInfo.getHaveRev().equals(syncInfo.getHeadRev()) && !syncInfo.getHeadAction().equals(ResourceSyncInfo.ACTION_DELETE));
		}
		
		return result;
	}

	public static String[] fileNames(LocalFile[] files)
	{
		List argumentList = new ArrayList();
		for (int i = 0, length = files.length; i < length; i++)
			argumentList.add(files[i].getAbsolutePath());
		
		return (String[])argumentList.toArray(new String[argumentList.size()]);		
	}
	
}
